<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* 
*/
class MY_Model extends CI_Model {
	public $error = '';

	public function __construct() {
		parent :: __construct();
		$this->load->database();
	}

	public function get_data($table = NULL)
	{
		// 返回表字段元数据
		$fields = $this->db->list_fields($table);
		// 接收post数据
		$post = $this->input->get_post(NULL, TRUE);
		
		foreach ($fields as $key => $value) {
			switch ($value) {
				case 'update_time':
					$post[$value] = time();
					break;

				case 'status':
					$post[$value] = empty($post[$value]) ? 0 : 1;
					break;

				case 'create_time':
					$this->router->method == 'add' AND $post[$value] = time();
					break;

				case 'create_ip':
					$this->router->method == 'add' AND $post[$value] = $this->input->ip_address();
					break;

				default:
					# code...
					break;
			}
		}
		if (empty($post['password'])) {
			unset($post['password']);
		} else {
			$post['password'] = $this->router->class == 'user' ? user_password($post['password']) : member_password($post['password']);
		}
		foreach ($post as $key => $value) {
			if (! in_array($key, $fields)) {
			    unset($post[$key]);
			    continue;
			}
			$post[$key] = is_string($value) ? trim($value) : $value;
		}
		unset($fields);
		return $post;
	}

	/**
	 * 清空
	 * @return [boolean]
	 */
	public function truncate($table = '')
	{
		if (empty($table)) {
			return NULL;
		}
		return $this->db->truncate($table);
	}
}